import QtQuick
import "../../framework"

//从键盘中读取按键，通过wasd控制移动

Item{
    property InputComponent input

    property vector3d move:getOutput()//移动方向

    function getOutput(){
            var ret = Qt.vector3d(0,0,0)
            if(direction & WasdController.MoveRight)
            {
                ret.x -= 1
            }
            if(direction & WasdController.MoveLeft)
            {
                ret.x += 1
            }
            if(direction & WasdController.MoveForward)
            {
                ret.z += 1
            }
            if(direction & WasdController.MoveBackward)
            {
                ret.z -= 1
            }
            if(direction & WasdController.Up)
            {
                ret.y += 1
            }

            return ret
    }

    enum Direction
    {
        None = 0,
        MoveForward = 1,
        MoveBackward = 2,
        MoveLeft = 4,
        MoveRight = 8,
        Up = 0x10
    }
    property int direction:WasdController.None

    Connections{
        target:input && input.mainMouseArea?input.mainMouseArea.Keys:null

        function onPressed(e)
        {
            if(!e.isAutoRepeat)
            {
                if(e.key === Qt.Key_D)
                {
                    direction = direction | WasdController.MoveRight
                }
                else if(e.key === Qt.Key_W)
                {
                    direction = direction | WasdController.MoveForward
                }
                else if(e.key === Qt.Key_S)
                {
                    direction = direction | WasdController.MoveBackward
                }
                else if(e.key === Qt.Key_A)
                {

                    direction = direction | WasdController.MoveLeft
                }
                else if(e.key === Qt.Key_Space)
                {
                    direction = direction | WasdController.Up
                }
            }
        }

        function onReleased(e)
        {
            if(!e.isAutoRepeat)
            {
                if(e.key === Qt.Key_D)
                {
                    direction = direction & ~WasdController.MoveRight
                }
                else if(e.key === Qt.Key_W)
                {
                    direction = direction & ~WasdController.MoveForward
                }
                else if(e.key === Qt.Key_S)
                {
                    direction = direction & ~WasdController.MoveBackward
                }
                else if(e.key === Qt.Key_A)
                {
                    direction = direction & ~WasdController.MoveLeft
                }
                else if(e.key === Qt.Key_Space)
                {
                    direction = direction & ~WasdController.Up
                }
            }
        }

    }

}
